﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Sockets;
using SMS_Server;
using System.Net;
using System.IO;

namespace SMS_Server_Manager
{
    public static class Global
    {
        public static string Licensed_to;

        public static string CompanyName;

        public static string Email;

        public static string SerialNumber;

        public static bool IsTrial;

        public static DateTime StartDate;

        public static DateTime ExpiryDate;

        public static bool IsValidSerial;

        public static string IMEI;

        public static string AllowedCountryCodes;

        public static bool unsaved_changes = false;

        public static List<Function> Functions = new List<Function>();

        public static string ProductID = "SMSSERVER2011.13";

        public readonly static string DatabasePassword = "Agl#@463687";

        public static string GetUsageDatabaseName()
        {
            return "Usages";
        }

        public static string GetConfigurationDatabaseName()
        {
            return "ManageConfig";
        }

        public static System.Net.IPAddress GetDefaultIP()
        {
            string s = System.Net.Dns.GetHostName();
            System.Net.IPAddress[] ips = System.Net.Dns.GetHostEntry(s).AddressList;
            System.Net.IPAddress Ip = ips[0];
            foreach (System.Net.IPAddress i in ips)
            {
                string ipaddress = i.ToString();
                if (ipaddress.IndexOf('.') < 0) continue;
                Ip = i;
            }
            return Ip;
        }

        private static bool SaveBack()
        {
            string path = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
            if (path[path.Length - 1] == '\\') path = path.Substring(0, path.Length - 1);
            try
            {
                //string MachineID = MachineIdentity.GetMachineIdentity();
                FileStream Sourcefs = new FileStream(path+"\\App_data\\ManageConfig.sdf", FileMode.Open, FileAccess.Read);
                FileStream Destinationfs = new FileStream(path+"\\config\\config.src", FileMode.Create, FileAccess.Write);

                int counter = 0;
                long totall = Sourcefs.Length;
                long tillnow = 0;
                byte[] buffer = new byte[1024];

                //FileSecurity file_security = new FileSecurity();

                while (true)
                {
                    counter = Sourcefs.Read(buffer, 0, buffer.Length);
                    if (counter == 0) break;

                    if (counter == buffer.Length)
                    {
                        //byte[] newBuff = file_security.DeltaMultiply(buffer, MachineID);
                        Destinationfs.Write(buffer, 0, counter);
                    }
                    else
                    {
                        byte[] secbuff = new byte[counter];
                        for (int i = 0; i < counter; i++)
                        {
                            secbuff[i] = buffer[i];
                        }
                        //byte[] newBuff = file_security.DeltaMultiply(secbuff, MachineID);
                        Destinationfs.Write(secbuff, 0, counter);
                    }
                    tillnow += counter;
                }
                Destinationfs.Close();
                Sourcefs.Close();
            }
            catch
            {
                return false;
            }
            return true;
        }

    }
}
